package process;

import java.io.IOException;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import protocol.funccode.FcCommandException;
import protocol.funccode.registers.RegTblParserException;
import protocol.iec104.IEC104Command;
import protocol.iec104.IEC104FrameEncode;
import protocol.iec104.IFrameStruct;
import task.iec104.IEC104SendTask;
import util.DataUtil;
import util.MyByteArrayOutputStream;
import core.IEC104Server;
import core.ProcessThread;
import core.scheduledtask.getFBDataTask;


public class IEC104Processor {

	public IEC104Processor() {
		// TODO Auto-generated constructor stub
	}
	private static Logger logger = Logger.getLogger(IEC104Processor.class);
	
	public final static IEC104Processor INSTANCE = new IEC104Processor();
	public void processRtData(IEC104Command command, ProcessThread thd) throws RegTblParserException, SQLException, FcCommandException{
		if (command.getTransReason()==6){
			logger.info("1");
			IFrameStruct ist= new IFrameStruct(thd.getSendIndex(),thd.getRcvIndex(),100,1,7,1,0,new byte[]{20});
			byte[] iframe=IEC104FrameEncode.genIframe(ist);
			logger.info(DataUtil.BytesToHexStr(iframe));
			thd.sendIframe(iframe);
			genYXZZtask(thd);
			genYCZZtask(thd);
			IEC104Server.tmZZ.cancel();
			
			IEC104Server.tmFB.schedule(new getFBDataTask(thd), 5000,60000);
			//IEC104.worker.feedHiTask(new IEC104SendTask(thd, new byte[]{61,62,63}));
			//IEC104.worker.feedTask(new IEC104SendTask(thd, new byte[]{51,52,53}));
			
			
			//new byte[]{}
			//response 
			//total data
			//how to get total data
			//计划任务，实时更新数据，有总召唤指令，临时打包数据，到普通发送队列
			//定时更新数据时，如果发现有变化，则添加到高优先队列
			//有总召唤才启动变化数据发送任务
			//有总召才启动数据查询？
			//定时任务1，低频率，更新map，准备打包的数据，定时任务2，高频率，发现变化遥信数据，如果发现了，高优先级发送，任务2启动时任务1停止
			//end frame
			   
		}
	}
	
	public void processTestData(IEC104Command command, ProcessThread thd) throws RegTblParserException, SQLException, FcCommandException{
		//if (command.getTransReason()==6){
			logger.info("67");
			
			
			
			   
		//}
	}
	
	private void genYXZZtask(ProcessThread thd){
		logger.info("start gen iframetask");
		int points=20000;
		int count= 127;
		byte[] data;
    	MyByteArrayOutputStream output = new MyByteArrayOutputStream();
		output.reset();
		for (int j=0;j<((points/count)+1);j++){
			output.reset();
			int firstaddr=0;
			for (int i=0;i<count;i++){
				
				int addr= count*j+i;
				if (i==0){
					firstaddr=addr;
				}
				
				String v=IEC104Server.yxdata.get(String.valueOf(addr));
				if (v==null){
					output.write(0);
				}else{
				    int iv=Integer.parseInt(v);
				    output.write(iv);
				}
			}
		    data=output.toByteArray();
		    IFrameStruct ist= new IFrameStruct(thd.getSendIndex(), thd.getRcvIndex(), 100, 255, 20, 1, firstaddr, data);
		    IEC104Server.worker.feedTask(new IEC104SendTask(thd, ist));
			
		}
		
		
	}
	private void genYCZZtask(ProcessThread thd){
		logger.info("start gen yczz iframetask");
		int points=20;
		int count= 2;
		byte[] data;
    	MyByteArrayOutputStream output = new MyByteArrayOutputStream();
		output.reset();
		for (int j=0;j<((points/count)+1);j++){
			output.reset();
			int firstaddr=0;
			for (int i=0;i<count;i++){
				
				int addr= count*j+i+11781;
				if (i==0){
					firstaddr=addr;
				}
				
				String v=IEC104Server.ycdata.get(String.valueOf(addr));
				if (v==null){
					try {
						output.write(new byte[]{0,0,0,0,0});
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
				   // int iv=Integer.parseInt(v);
					float fv= Float.parseFloat(v);
					byte[] tmp= DataUtil.float2byte(fv);
				    try {
						output.write(tmp);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					output.write(0);
				}
			}
		    data=output.toByteArray();
		    IFrameStruct ist= new IFrameStruct(thd.getSendIndex(), thd.getRcvIndex(), 21, count+128, 20, 1, firstaddr, data);
		    IEC104Server.worker.feedTask(new IEC104SendTask(thd, ist));
			
		}
		
		
	}

}
